﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.MediaCenter.UI;

namespace Simplicity
{
    //******************************************************************************************************************
    //  This class is used to house your configuration parameters.  It is a decendant of ModelItem and will be passed 
    //  into your config panel at runtime (see configpanel setup in plugin.cs) so that you can bind to its properties.
    //******************************************************************************************************************
    public class Data
    {


        public bool enableNPV;
        public bool enableVideoBackdrop;
        public string subTheme;
        public Single backdropTransitionTime;

    }


    public class MyConfig : ModelItem
    {
        private static Data data;
        private static string SettingPath = MediaBrowser.Library.Configuration.ApplicationPaths.AppPluginPath +
                                             "\\configurations\\SimplicitySettings.xml";
        
        public MyConfig()
        {
            if (data == null)
            {
                data = new Data();
                InitSettings();
            }

        }

        public static void InitSettings()
        {
            //SettingPath = Path.GetFullPath();
            if (File.Exists(SettingPath))
            {
                LoadSettings();
            }
            else
            {
                data.enableNPV = true;
                data.enableVideoBackdrop = true;
                data.subTheme = "Default";
                data.backdropTransitionTime = 0.5f;
                SaveSettingsFile();
            }
        }

        private static void LoadSettings()
        {
            XmlSerializer ser = new XmlSerializer(typeof(Simplicity.Data));

            TextReader reader = new StreamReader(SettingPath);
            data = (Simplicity.Data)ser.Deserialize(reader);
            reader.Close();
        }

        public static void SaveSettingsFile()
        {
            XmlSerializer ser = new XmlSerializer(typeof(Simplicity.Data));

            TextWriter writer = new StreamWriter(SettingPath);
            ser.Serialize(writer, data);
            writer.Close();

        }



        public bool EnableNPV
        {
            get { return data.enableNPV; }
            set
            {
                if (data.enableNPV != value)
                { data.enableNPV = value; FirePropertyChanged("EnableNPV"); SaveSettingsFile(); }
            }
        }
        public bool EnableVideoBackdrop
        {
            get { return data.enableVideoBackdrop; }
            set
            {
                if (data.enableVideoBackdrop != value)
                { data.enableVideoBackdrop = value; FirePropertyChanged("EnableVideoBackdrop"); SaveSettingsFile(); }
            }
        }
        public string SubTheme
        {
            get { return data.subTheme; }
            set
            {
                if (data.subTheme != value)
                { data.subTheme = value; FirePropertyChanged("SubTheme"); SaveSettingsFile(); }
            }
        }

        public Single BackdropTransitionTime
        {
            get { return data.backdropTransitionTime; }
            set
            {

                if (data.backdropTransitionTime != value)
                { data.backdropTransitionTime = (float)Math.Round(value, 1, MidpointRounding.ToEven); FirePropertyChanged("BackdropTrnsitionTime"); SaveSettingsFile(); }
            }
        }

    }
}
